package com.restaurant.validators;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.restaurant.domain.Menu;
import com.restaurant.domain.Sidedishmenu;
import com.restaurant.service.SidedishmenuService;

public class MenuItemFormDeleteValidator implements Validator{

	@Autowired	
	private SidedishmenuService objSidedishmenuService;
	
	@Override
	public boolean supports(Class<?> arg0) {
		return Menu.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object arg0, Errors errors) {
		Menu menu = (Menu) arg0;
		
		ArrayList<Sidedishmenu> listSidedishmenus = new ArrayList<Sidedishmenu>();
		
		try {
			listSidedishmenus = (ArrayList<Sidedishmenu>) objSidedishmenuService.loadByHql("from Sidedishmenu where menu1.menuID = " + menu.getMenuID());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(listSidedishmenus.size()>0){
			errors.rejectValue("menu.alreadyExists", "menu.exists");
		}
	}

}
